//聚品牌-首页
function jpp_i() {
    new Vue({
        el: "#jpp_i",
        data() {
            return {
                list: [],
                bannerlist: [],
                searchtext: null,
                followId:{"":{},"":{}}, //关注的品牌id
                deleteFollowId:[] //取消关注的品牌id
            }
        },
        methods: {
            getData() {
                let that = this;
                //获取品牌列表
                axios.get(urlParam.domain() + '/api/brand/brand/recommend', {
                    params: {
                        num: 9, //每页数据量
                        pageNo: 1,  //页码
                        brandName: that.searchtext,
                        token: urlParam.token()
                    }
                }).then(function(response) {
                    if (response.data.data != undefined) {
                        that.list = response.data.data;
                        console.log(that.list);
                    }

                });
            },
            doConcern:function (data) {
                if (urlParam.token() == undefined || urlParam.token() == '') {
                    alert('请先登录');
                    return;
                }
                let followId = data.followId ;
                if (followId==0){

                }
                let reqType = data.followId==0?"add":"delete";
                axios({
                    method:"post",
                    url:urlParam.domain() + "/api/brand/follow/" + reqType,
                    headers:{"token": urlParam.token()},
                    data:{
                        id: data.followId,
                        brandId: data.id,
                        designerId: this.designerId,
                    },
                    transformRequest: [function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        return ret
                    }]
                }).then(res =>{
                    if(reqType=="delete"){
                        data.followId=0;
                    }else {
                        console.log("followId:", res.data.data);
                        data.followId=res.data.data;
                    }
                }).catch(res =>{
                    console.log("doConcern fail!");
                })
            },
            //获取顶部搜索框内容
            getSearchTxt(val) {
                this.searchtext = val;
                //获取数据
                this.getData();
            },
            getAdvert() {
                let that = this;
                //获取广告
                axios.get(urlParam.domain() + '/api/advert/list', {
                    params: {
                        position: urlParam.jpinpaiPosition()
                    }
                }).then(function(response) {
                    that.bannerlist = response.data.data;
                });
            }
        },
        created() {
            this.getAdvert();
            this.getData();
        },
        components: {
            'common-swiper': common_swiper,
            'common-biaoti': common_biaoti
        }
    });
}

function formartPostParms(data) {
    var d = '';
    if (data instanceof Object) {
        for (var x in data) {
            if (data[x] === '') {
                continue;
            }
            d += '&' + x + '=' + data[x];
        }
    }
    return d.substr(1);
}

//聚品牌-品牌动态
function jpp_dongtai() {
    new Vue({
        el: "#jpp_dongtai",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '品牌动态'
                }],
                'pageNo': 1,
                'pageSize': 10,
                'total': 0,
                'list': null,
                detailServerUrl:urlParam.location()+"/jpinpai/dongtai_detail.html?id=",
            }
        },
        methods: {
            getData() {
                let that = this;
                axios.get(urlParam.domain() + '/api/brand/dynamics/list', {
                    params: {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize
                    }
                }).then(function(response) {
                    console.log(response)
                    if (response.data.data != undefined) {
                        that.list = response.data.data.list;
                        that.total = Math.ceil(response.data.data.total / that.pageSize);
                    }
                });
            },
            listen(data) {
                this.pageNo = data;
                this.created();
            },
            targetDetail(id){
                console.log("url:",this.detailServerUrl +", id:"+id);
                setTimeout(window.location=this.detailServerUrl+id,0);
            }
        },
        created() {
            this.getData();
        }
    });
}

//聚品牌-品牌动态-详情
function jpp_dongtai_detail() {
    let mockData = Mock.mock({
        ppinfo: {
            logo: '@image(86x86,#abcdef)',
            foundingTime: '2018-12-20',
            companyName: '义务市思思楠陶服饰有限品牌',
            brandName: '佛山市顺德轩日服饰有限公司'
        }
    })
    new Vue({
        el: "#jpp_dongtai_detail",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '品牌动态'
                }, {
                    'href': '',
                    'text': 'ARA的快时尚之路'
                }],
                brandInfo: {},
                id : -1, //品牌动态id
                title: '2017年敬请关注冬季ZARA的快时尚之路',
                content: '这里是HTML内容',
                from: '聚衣服饰',
                date: '2017-10-12',
                severBaseUrl : urlParam.location()+"/jpinpai/dongtai_detail.html?id=",
                prevEle:{
                    id:"",
                    title:"暂无更早动态",
                    serviceUrl:"#"
                },
                nextEle:{
                    id:"",
                    title:"暂无更多动态",
                    serviceUrl:"#"
                }
            }
        },
        methods: {
            getData() {
                this.id ==-1?null:this.id;
                console.log("id:",this.id);
                axios.get (urlParam.domain()+"/api/brand/dynamics/detail",{
                    params: {
                        'token':urlParam.token(),
                        "id": this.id
                    }
                }).then(res =>{ 
                    let objLinks= res.data.data;
                    console.log(objLinks)
                    objLinks.forEach(item => {
                        if (item.dataType =="data"){
                            if (item.brandInfo != null){
                                this.from = item.brandInfo.companyName;
                            }
                            this.brandInfo = item.brandInfo;
                            this.id = item.id;
                            this.title = item.title;
                            this.content = item.content;
                            this.date = item.createTime;
                            this.crumblist[3] = {
                                'href':this.serverUrl+item.id,
                                'text':this.title
                            }
                        }else if (item.dataType =="prev"){
                            this.prevEle.id = item.id;
                            this.prevEle.title = item.title;
                            this.prevEle.serviceUrl = this.severBaseUrl+item.id;
                        }else if (item.dataType =="next"){
                            this.nextEle.id = item.id;
                            this.nextEle.title = item.title;
                            this.nextEle.serviceUrl = this.severBaseUrl+item.id;
                        }
                    });

                }).catch(res =>{
                    console.log("res is fial!!")
                })
            },

        },
        created() {
            let id = globalAPI.getRequest().id;
            if(id!=null){
                this.id = id;
            }
            this.getData()
        }
    });
}

//聚品牌-品牌列表[搜索]
function jpp_pinpai_list() {
    let mockData = Mock.mock({
        'catelist': [{
            'catename': 'fenlei',
            'title': '分类',
            'arr|5': [{
                'txt': '@ctitle(2,4)',
                'val|+1': 1,
            }]
        }]
    });
    new Vue({
        el: "#jpp_pinpai_list",
        data() {
            return {
                where: {
                    daterange: '',
                    cateobj: {}
                },
                'crumblist': {
                    'crumb': [{
                        'href': '',
                        'text': '首页'
                    }],
                    'arr': [] //显示选中的搜索条件
                },
                'list': null,
                'catelist': null,
                'pageNo': 1,
                'pageSize': 10,
                'total': 1,
                'startFoundTime': null,
                'endFoundTime': null,
                'startAge': null,
                'endAge': null,
                'labelId': null,
                'searchtext': null
            }
        },
        methods: {
            getData() {
                this.getLabelList();
                this.getBrandList();
            },
            doConcern(data) {
                if (urlParam.token() == undefined || urlParam.token() == '') {
                    this.$notify({
                        title: '错误',
                        message: "请先登录账户",
                        type: 'error'
                    })
                    return;
                }
                let followId = data.followId ;
                let reqType = data.followId==0?"add":"delete";
                axios({
                    method:"post",
                    url:urlParam.domain() + "/api/brand/follow/" + reqType,
                    headers:{"token": urlParam.token()},
                    data:{
                        id: data.followId,
                        brandId: data.id,
                    },
                    transformRequest: [function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        return ret
                    }]
                })
                    .then(res =>{
                    if(reqType=="delete"){
                        data.followId=0;
                    }else {
                        data.followId=res.data.data;
                    }
                })
            },
            // 设置分类筛选的初始条件
            initWhere() {
                let cateobj = {};
                if (this.catelist != null) {
                    for (let i = 0; i < this.catelist.length; i++) {
                        cateobj[this.catelist[i].catename] = '';
                    }
                    this.where.cateobj = cateobj;
                }
            },
            getWhere(obj) {
                this.where = obj;
                this.startAge = this.where.minage;
                this.endAge = this.where.maxage;
                if (this.where.daterange != undefined) {
                    var len = this.where.daterange.length;
                    if (len >= 1) {
                        this.startFoundTime = this.where.daterange[0];
                        if (len == 2) {
                            this.endFoundTime = this.where.daterange[1];
                        }
                    }
                }
                this.crumblist.arr = [];
                if (this.where.cateobj != undefined) {
                    this.crumblist.arr.push(this.where.cateobj);
                    this.labelId = this.where.cateobj.val;
                } else {
                    this.labelId = null;
                }
                this.getBrandList();
            },
            getCrumblist(data) {
                this.crumblist = data;
                this.labelId = null;
                this.where.cateobj = {};
                this.getBrandList();
            },
            getPage(data) {
                this.pageNo = data;
                this.getData();
            },
            //获取顶部搜索栏信息
            getLabelList() {
                var that = this;
                axios.get(urlParam.domain() + '/api/brand/label/getLabelList', {
                    params: {}
                }).then(function(response) {
                    if (response.data.data != undefined) {
                        // console.log(response.data.data)
                        let obj = {};
                        obj.title = "品牌风格";
                        obj.catename = 'fenlei';
                        obj.arr = response.data.data;
                        that.catelist = [];
                        that.catelist.push(obj);
                    }

                });
            },
            //发送搜索条件 获取信息列表
            getBrandList() {
                let that = this;
                let method = "";
                if (urlParam.token() == undefined || urlParam.token() == '') {
                    method = 'get';
                }else {
                    method ='post';
                }
                axios({
                    method:method,
                    headers:{'token':urlParam.token()},
                    url:urlParam.domain() + '/api/brand/brand/list',
                    params: {
                        'pageNo': that.pageNo,
                        'pageSize': that.pageSize,
                        'startAge': that.startAge,
                        'endAge': that.endAge,
                        'startFoundTime': that.startFoundTime,
                        'endFoundTime': that.endFoundTime,
                        'labelId': that.labelId,
                        'brandName': that.searchtext,
                    }
                }).then(function(response) {
                    if (response.data.data != undefined) {
                        that.list = response.data.data.list;
                        that.total = Math.ceil(response.data.data.total / that.pageSize);
                    }
                });
            },
            //获取顶部搜索框内容
            getSearchTxt(val) {
                this.searchtext = val;
                //获取数据
                this.getBrandList();
            }
        },
        created() {
            let tag = globalAPI.getRequest().q;
            if(tag!=null && tag!=undefined && tag!=''){
                this.searchtext = tag;
                let obj = {
                    href:'#',
                    text:this.searchtext
                }
                this.crumblist.crumb.push(obj);
            }
            this.getData();
            this.initWhere();
        },
        components: {
            'shaixuan-bar': jpp_pp_shaixuan_bar
        }
    });
}

//聚品牌-品牌详情
function jpp_pinpai_detail() {
    new Vue({
        el: "#jpp_pinpai_detail",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '品牌介绍'
                }],
                content: '这里是HTML内容',
                id:"",
            }
        },
        methods: {
            getData() {
                let that = this;
                axios.get(urlParam.domain()+"/api/brand/brand/detail",{
                    params:{token:urlParam.token(),
                    id:that.id}
                }).then(res => {
                    let brandObj = res.data.data.brand;
                    that.id = brandObj.id;
                    that.content = brandObj.content;
                })
            },
        },
        created() {
            this.id = globalAPI.getRequest().id;
            this.getData()
        }
    });
}

//聚品牌-形象展示
function jpp_xx_show() {
    new Vue({
        el: "#jpp_xx_show",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '形象展示'
                }],
                'pageNo': 1,
                'pageSize': 10,
                'list': null,
                'total': 0,
                'searchtext': null,
                'brandId': null,
            }
        },
        methods: {
            getData() {
                let that = this;
                axios.get(urlParam.domain() + '/api/brand/image/list', {
                    params: {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        title: that.searchtext,
                        brandId:that.brandId,
                    }
                }).then(function(response) {
                    if (response.data.data != undefined) {
                        that.list = response.data.data.list;
                        that.total = Math.ceil(response.data.data.total / that.pageSize);
                        console.log(that.list);
                    }
                });
            },
            getPage(data) {
                this.pageNo = data;
                this.created();
            },
            //获取顶部搜索框内容
            getSearchTxt(val) {
                this.searchtext = val;
                //获取数据
                this.getData();
            }
        },
        created() {
            this.brandId = globalAPI.getRequest().id;
            this.getData();
        }
    });
}

//聚品牌-形象展示-详情
function jpp_xx_show_detail() {
    new Vue({
        el: "#jpp_xx_show_detail",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '形象展示'
                }, {
                    'href': '',
                    'text': '详情'
                }],
                brandInfo: {},
                ppinfo: {},
                id : null,
            }
        },
        mounted() {
            this.initBanner();
            // this.detail();
        },
        methods: {
            getData() {
                let id = this.id;
                let that = this;
                axios.get(urlParam.domain() + '/api/brand/image/detail', {
                    params: {
                        'token':urlParam.token(),
                        'id':id
                    }
                }).then(function(response) {
                    let obj = response.data.data;
                    console.log("res :",response.data.data);
                    that.brandInfo = obj.image;
                    that.brandInfo.picUrl = obj.image.picUrl.split(',');
                    that.ppinfo = obj.brand;
                });
            },
            initBanner() {
                var galleryTop = new Swiper('.gallery-top', {
                    spaceBetween: 10,
                    loop: true,
                    loopedSlides: 5, //looped slides should be the same
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                });
                var galleryThumbs = new Swiper('.gallery-thumbs', {
                    spaceBetween: 10,
                    slidesPerView: 4,
                    touchRatio: 0.2,
                    loop: true,
                    loopedSlides: 5, //looped slides should be the same
                    slideToClickedSlide: true,
                });
                galleryTop.controller.control = galleryThumbs;
                galleryThumbs.controller.control = galleryTop;


            }
        },
        created() {
            this.id = globalAPI.getRequest().id;
            this.getData()
        }
    });
}

//聚品牌-联系我们
function jpp_contact_us() {
    jpp_contact_us = new Vue({
        el: "#jpp_contact_us",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '联系我们'
                }],
                'sjinfo': {
                    companyName:"",
                    companyAddress:"",
                    phone:"",
                    qq:"",
                    weChat:"",
                    weibo:""
                }
            }
        },
        methods: {
            getData() {
                let id = globalAPI.getRequest().id;
                var that = this;
                axios.get(urlParam.domain() + '/api/brand/contactUs/getContactUs', {
                    params: {
                        brandId: id
                    }
                }).then(function(response) {
                    if (response.data.data != undefined) {
                        that.sjinfo = response.data.data;
                    }
                });
            }
        },
        created() {
            this.getData();
        }
    });
}

//聚品牌-招商加盟
function jpp_jiameng() {
    new Vue({
        el: "#jpp_jiameng",
        data() {
            return {
                'crumblist': [{
                    'href': '',
                    'text': '首页'
                }, {
                    'href': '',
                    'text': '聚品牌'
                }, {
                    'href': '',
                    'text': '招商加盟'
                }],
                content: '这里是HTML内容',
                area: [],
                cityList: [],
                areaList: [],
                list: [],
                provinceIndex: '',
                cityIndex: '',
                leagueApply: {},
                brandName: '',
                parentId: 0,
                followId : 0,
                rules: {
                    contacts: [
                        { required: true, message: '请填写联系人', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请填写联系手机', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择您的企业类型', trigger: 'change' }
                    ],
                    status: [
                        { required: true, message: '请问您是否加盟过其他品牌', trigger: 'change' }
                    ],
                    remarks: [
                        { required: true, message: '请填写留言内容', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                        let that = this;
                        console.log(that.leagueApply);
                        axios.post(
                            urlParam.domain() + '/api/brand/leaqueApply/add',
                            JSON.stringify(that.leagueApply), { headers: { "token": urlParam.token(), "Content-Type": "application/json" } }
                        ).then(result => {
                            that.$notify({
                                title: '成功',
                                message: '提交加盟申请成功',
                                type: 'success'
                            });
                            if (result.data.code == 1) {
                                this.leagueApply = {};
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            getList() {
                let that = this;
                axios.get(urlParam.domain() + '/api/area/list', {
                    params: {
                        parentId: that.parentId
                    }
                }).then(function(response) {
                    if (response.data.code == 1) {
                        that.list = response.data.data;
                    }
                });
            },
        },
        created() {
            let that = this;
            this.id = globalAPI.getRequest().id;

            axios.get(urlParam.domain() + '/api/brand/brand/detail',{
                params: {id:this.id,token:urlParam.token()}
            }).then(res => {
                let obj = res.data.data.brand;
                console.log("obj",res.data.data);
                this.leagueApply.brandId = obj.id;
                this.brandName = obj.brandName;
                this.condition = obj.condition;
                this.followId = obj.followId;
            });
            axios.get(urlParam.domain() + '/api/area/listAll', {
                params: {}
            }).then(function(response) {
                if (response.data.code == 1) {
                    that.area = response.data.data;
                }
            });
        },
        watch: {
            // 监听省发生变化
            provinceIndex: {
                handler: function() {
                    //清空市和区
                    this.cityList = [];
                    this.areaList = [];
                    this.cityId = '';
                    //赋值当前的省id
                    this.leagueApply.provinceId = this.area[this.provinceIndex].id;
                    //获取当前省的下级市
                    this.cityList = this.area[this.provinceIndex].children;
                },
            },
            // 监听市发生变化
            cityIndex: {
                handler: function() {
                    //清空区
                    this.areaList = [];
                    this.areaId = '';
                    //赋值当前的区id
                    this.leagueApply.cityId = this.cityList[this.cityIndex].id;
                    //获取当前市的下级区
                    this.areaList = this.cityList[this.cityIndex].children;
                },
            }
        }
    });
}

//聚品牌-新品上市
function jpp_xinpin_list() {
    let mockData = Mock.mock({
        'prolist|8': [{
            'picUrl': '@image(255x255, #FFC0CB)',
            'goodsName': '@ctitle(10, 15)',
            'id|+1': 1,
            'unitPrice|50-100': 1,
            'minNum|50-100': 1,
        }],
    });
    new Vue({
        el: "#jpp_xinpin_list",
        data() {
            return {
                prolist: [],
                pageNo: 1,
                pageSize: 12,
                total: 0,
                status: 1,
                crumblist: [{
                    text: "首页",
                    href: "#"
                }, {
                    text: "聚品牌",
                    href: "#"
                }, {
                    text: "新品上市",
                    href: "#"
                }],
                brandId:null
            }
        },
        methods: {
            getData() {
                this.getShopGoods();
            },
            getPage(page) {
                this.pageNo = page;
                this.getData();
            },
            doConcern(data){
                if (urlParam.token() == undefined || urlParam.token() == '') {
                    this.$notify({
                        title: '错误',
                        message: "请先登录账户",
                        type: 'error'
                    })
                    return;
                }
                let followId = data.followId ;
                let reqType = data.followId==0?"add":"delete";
                axios({
                    method:"post",
                    url:urlParam.domain() + "/api/brand/follow/" + reqType,
                    headers:{"token": urlParam.token()},
                    data:{
                        id: data.followId,
                        brandId: data.id,
                    },
                    transformRequest: [function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        return ret
                    }]
                })
                    .then(res =>{
                        if(reqType=="delete"){
                            data.followId=0;
                        }else {
                            data.followId=res.data.data;
                        }
                    })
            },
            //查询商品列表
            getShopGoods(){
                let that = this;
                let method = "";
                if (urlParam.token() == undefined || urlParam.token() == '') {
                    method = 'get';
                }else {
                    method ='post';
                }
                axios({
                    method:method,
                    headers:{'token':urlParam.token()},
                    url:urlParam.domain()+"/api/shop/goods/list",
                    params:{
                        'pageNo': this.pageNo,
                        'pageSize': this.pageSize,
                        'brandId': this.brandId,
                        'status': this.status
                    }
                }).then(res =>{
                    let pageData = res.data.data;
                    this.total = Math.ceil(pageData.total / that.pageSize);
                    this.prolist = pageData.list;
                }).catch(res =>{
                    that.$notify({
                        title:"错误",
                        type:"error",
                        message:res.data.data
                    })
                })
            },
        },
        created() {
            let obj = globalAPI.getRequest();
            this.brandId = obj.id;
            this.getData();
        },
        components: {
            'base-pro-list': base_pro_list,
        }
    });
}